package com.zhende.hr.controller;

import java.util.Calendar;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.algorithms.Algorithm;
import com.zhende.hr.constant.Constans;
import com.zhende.hr.entity.KqUser;
import com.zhende.hr.mapper.kqlog.KaoQinDao;
import com.zhende.hr.utils.JWTUtils;
import com.zhende.hr.utils.R;


/**
 * 获取考勤
 */
@RestController
@RequestMapping("/u")
@CrossOrigin
public class KqController {

	@Autowired
	private KaoQinDao kaoQinDao; 


	
	
	@GetMapping("/getKq")
	public R getToken(@RequestParam(value = "perCdcode", required = true) String perCdcode ) {
		if (StringUtils.isBlank(perCdcode) ) {
			return R.error("请输入员工编号");
		}
		List<KqUser> users = kaoQinDao.getUsers(perCdcode);
		 R r = R.ok();
		 r.put(R.datas, users);
		return r;
	}
	
	public static void main(String[] args) {
		String name = "aa";
		String password = "bb";
		Map<String, String> map = new Hashtable<>();
		map.put("type", Constans.auth_INTERFACE);
		map.put("uuid", UUID.randomUUID().toString());  // 添加唯一标识符
		map.put("name", name);
//		map.put("password", password);
		map.put("time", String.valueOf(System.currentTimeMillis()));
		String token = JWTUtils.getToken(map);
		System.out.println(token);
		map.put("uuid", UUID.randomUUID().toString());  // 添加唯一标识符
		map.put("time", String.valueOf(System.currentTimeMillis()+3333333l));
		token = getToken(map);
		System.out.println(token);
	}
    private static final String SING = "fmf123456";
    public static String getToken(Map<String,String> map){
        Calendar instance = Calendar.getInstance();
        //默认7天过期
        instance.add(Calendar.HOUR,2);
        //创建jwt builder
        JWTCreator.Builder builder = JWT.create();
        map.forEach((k,v)->{
            builder.withClaim(k,v);
        });
        String token = builder.withExpiresAt(instance.getTime())
                .sign(Algorithm.HMAC256(SING));
        return token;
    }

	
}
